﻿using System;
using System.Linq;
using System.Threading.Tasks;

namespace Athena.RouteAddress.Implementations
{
    public abstract class BaseRouteSelector : IRouteSelector
    {
        ValueTask<Route> IRouteSelector.SelectAsync(ServiceRoute serviceRoute)
        {
            if (serviceRoute == null)
            {
                throw new ArgumentNullException(nameof(serviceRoute));
            }
            if (string.IsNullOrWhiteSpace(serviceRoute.CorrelationId) || serviceRoute.Routes == null)
            {
                throw new ArgumentException(nameof(serviceRoute));
            }
            var count = serviceRoute.Routes.Count();
            if (count == 0)
            {
                throw new ArgumentException(nameof(serviceRoute.Routes));
            }
            if (count == 1)
            {
                return new ValueTask<Route>(serviceRoute.Routes.First());
            }

            return SelectAsync(serviceRoute);
        }

        protected abstract ValueTask<Route> SelectAsync(ServiceRoute serviceRoute);
    }
}
